package jdklearning.concurrent;


import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CallableAndFutureAndExecuteService {

	
	public void future1() {
		Callable<Integer> callable = new Callable<Integer>() {
            public Integer call() throws Exception {
            	Thread.sleep(3000);
                return new Random().nextInt(100);
            }
        };
        FutureTask<Integer> future = new FutureTask<Integer>(callable);
        new Thread(future).start();
        try {
        	//主线程会阻塞在future.get()上
        	System.out.println(future.get());
//            Thread.sleep(5000);// 可能做一些事情
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
	}
	
	
	
	public void future2() {
		Callable<Integer> callable = new Callable<Integer>() {

			@Override
			 public Integer call() throws Exception {
//            	Thread.sleep(3000);
                return new Random().nextInt(100);
            }
        };
        FutureTask<Integer> future = new FutureTask<Integer>(callable);
        new Thread(future).start();
        
        try {
        	//主线程会阻塞在future.get()上
        	System.out.println(future.get());
//            Thread.sleep(5000);// 可能做一些事情
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
	}
	
	
	
	public void future3() {
		Callable<Integer> callable = new Callable<Integer>() {

			@Override
			 public Integer call() throws Exception {
            	Thread.sleep(3000);
                return new Random().nextInt(100);
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        futureTask.run();
        try {
			System.out.println(futureTask.get());
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void testCallableAndFutureAndExecuteService(){
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		Callable<Integer> callable = new Callable<Integer>() {

			@Override
			 public Integer call() throws Exception {
            	Thread.sleep(3000);
                return new Random().nextInt(100);
            }
        };
        Future<Integer> future = executorService.submit(callable);
        try {
        	int result = future.get();
			System.out.println(result);
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		CallableAndFutureAndExecuteService callableAndFutureAndExecuteService = new CallableAndFutureAndExecuteService();
		
		callableAndFutureAndExecuteService.testCallableAndFutureAndExecuteService();
		
		callableAndFutureAndExecuteService.future3();
    }
}
